package org.jbit.news.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbit.news.biz.TopicsBiz;
import org.jbit.news.biz.impl.TopicsBizImpl;
import org.jbit.news.entity.Topic;

public class TopicServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String opr = request.getParameter("opr");
		TopicsBiz topicsBiz = new TopicsBizImpl();
		// 鑾峰彇搴旂敤涓婁笅鏂囪矾寰�
		String contextPath = request.getContextPath();
		if (opr.equals("update")) { // 鏇存柊涓婚
			String tid = request.getParameter("tid");
			String tname = request.getParameter("tname");
			Map<String, String> topic = new HashMap<String, String>();
			topic.put("tid", tid);
			topic.put("tname", tname);
			if (topicsBiz.updateTopic(topic) > 0) {
				out.print("<script type='text/javascript'>"
						+ "alert('宸茬粡鎴愬姛鏇存柊涓婚锛岀偣鍑荤‘璁よ繑鍥炰富棰樺垪琛�);" + "location.href='"
						+ contextPath + "/TopicServlet?opr=list';</script>");
			} else {
				out.print("<script type='text/javascript'>"
						+ "alert('鏇存柊涓婚澶辫触锛岀偣鍑荤‘璁よ繑鍥炰富棰樺垪琛�);" + "location.href='"
						+ contextPath + "/newspages/topic_list.jsp';	</script>");
			}
		} else if (opr.equals("list")) {//鏌ユ壘鎵�湁涓婚
			List<Topic> list = topicsBiz.getAllTopics();
			request.getSession().setAttribute("list", list);
			response.sendRedirect(contextPath + "/newspages/topic_list.jsp");
		} else if (opr.equals("add")) {// 娣诲姞涓婚
			String tname = request.getParameter("tname");
			Topic topic = topicsBiz.findTopicByName(tname);
			if (topic == null) {
				topicsBiz.addTopic(tname);
				List<Topic> list4 = topicsBiz.getAllTopics();
				request.getSession().setAttribute("list4", list4);
				out.print("<script type='text/javascript'>"
						+ "alert('褰撳墠涓婚鍒涘缓鎴愬姛锛岀偣鍑荤‘璁よ繑鍥炰富棰樺垪琛紒');"
						+ "location.href='topic_ control.jsp?opr=list';	"
						+ "</script>");
			}else{
				out.print("<script type='text/javascript'>"
						+ "alert('褰撳墠涓婚宸插瓨鍦紝璇疯緭鍏ヤ笉鍚岀殑涓婚锛�);"
						+ "location.href='../newspages/topic_add.jsp';	"
						+ "</script>");
			}
		}else if(opr.equals("del")){//鍒犻櫎涓婚
			String tid = request.getParameter("tid");
			int result=topicsBiz.deleteTopic(tid);
			if(result==1){
				List<Topic> list4 = topicsBiz.getAllTopics();	
				request.getSession().setAttribute("list4",list4);//鎵�湁鐨勪富棰�
				out.print("<script type='text/javascript'>"
						+ "alert('宸茬粡鎴愬姛鍒犻櫎涓婚锛岀偣鍑荤‘璁よ繑鍥炲師鏉ラ〉闈紒');"
						+ "location.href='topic_ control.jsp?opr=list';	"
						+ "</script>");
			}else if(result==-1){
				out.print("<script type='text/javascript'>"
						+ "alert('鍒犻櫎涓婚澶辫触锛佽鑱旂郴绠＄悊鍛樻煡鎵惧師鍥狅紒鐐瑰嚮纭杩斿洖鍘熸潵椤甸潰锛�);"
						+ "location.href='topic_ control.jsp?opr=list';	"
						+ "</script>");
			}else{
				out.print("<script type='text/javascript'>"
						+ "alert('璇ヤ富棰樹笅杩樻湁鏂囩珷锛屼笉鑳藉垹闄わ紒');"
						+ "location.href='topic_ control.jsp?opr=list';	"
						+ "</script>");
			}
		}
		out.flush();
		out.close();
	}

}
